<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/2
 * Time: 11:24
 */

namespace app\index\logic;


use app\common\model\Page;
use app\common\utils\Constant;
use think\Db;
use think\Exception;
use think\Log;

class RepairLogic
{
    static private $instance;
    static public function getInstance(){
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    /**
     * 添加报事报修
     * @param $userId 用户ID
     * @param $housePropertyId 房产ID
     * @param $remark 描述
     * @return mixed
     */
    public function addRepair($userId, $housePropertyId, $remark){
        $houseProperty = model('HouseProperty')->where(array('id'=>$housePropertyId))->find();
        if($houseProperty['user_id'] != $userId){
            return array('code'=>Constant::CODE_ERROR, 'msg'=>'该房产不在你的名下');
        }
        //添加数据
        $re = model('Repair')->insert([
            'user_id'           =>    $userId
            ,'village_id'       =>    $houseProperty['village_id']
            ,'create_time'      =>    time()
            ,'house_property_id'=>    $housePropertyId
            ,'remark'           =>    $remark
            ,'status'           =>    Constant::REPAIR_DEAL_WAIT
        ]);
        if(!$re){
            return array('code'=> Constant::CODE_ERROR, 'msg'=>'添加失败');
        }
        return array('code'=> Constant::CODE_SUCCESS, 'msg'=> '提交成功');
    }

    /**
     * 获取报修记录
     * @param $data
     * @return mixed
     */
    public function getRepairList($data){
        $map = [];
        if(!isset($data['page_size'])){
            $data['page_size'] = 10;
        }
        if(!isset($data['page_num'])){
            $data['page_num'] = 1;
        }
        if(isset($data['user_id']) && !empty($data['user_id'])){
            $map['user_id'] = $data['user_id'];
        }
        if(isset($data['house_property_id']) && !empty($data['house_property_id'])){
            $map['house_property_id'] = $data['house_property_id'];
            $house = true;
            $house_property = model('HouseProperty')
                ->where(array('id' => $data['house_property_id']))
                ->field('building_number,floor_number,room_number,detail')
                ->find();
        }else{
            $house = false;
            $house_property = '';
        }
        $result = $this->queryByMap($map,$data['page_num'],$data['page_size']);
        foreach ($result['list'] as $key=>&$value){
            if(!$house) {
                $value['house_property'] = model('HouseProperty')
                    ->where(array('id' => $value['house_property_id']))
                    ->field('building_number,floor_number,room_number,detail')
                    ->find();
            }else{
                $value['house_property'] = $house_property;
            }
        }
        unset($value);
        return $result;
    }

    private function queryByMap($map,$page_num,$page_size){
        $startno = ($page_num - 1) * $page_size;
        $list = model('Repair')
            ->where($map)
            ->limit($startno,$page_size)
            ->order('id','desc')
            ->select();
        $count = model('Repair')
            ->where($map)
            ->count();
        $result['list'] = $list;
        $page = new Page($page_num,$count,$page_size);
        $result['page'] = $page;
        return $result;
    }
}